/*#include "cJSON.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netdb.h>
#include <errno.h>



// 天气状况英文转中文映射表
const char* weather_en_to_cn(const char* en_weather) {
    if (!en_weather) return "未知";
    
    // 常见天气状况映射，可根据实际返回结果补充
    if (strcmp(en_weather, "clear") == 0) return "晴";
    if (strcmp(en_weather, "partly cloudy") == 0) return "多云";
    if (strcmp(en_weather, "cloudy") == 0) return "阴";
    if (strcmp(en_weather, "light rain") == 0) return "小雨";
    if (strcmp(en_weather, "moderate rain") == 0) return "中雨";
    if (strcmp(en_weather, "heavy rain") == 0) return "大雨";
    if (strcmp(en_weather, "thunderstorm") == 0) return "雷阵雨";
    if (strcmp(en_weather, "snow") == 0) return "雪";
    return en_weather; // 未匹配到则返回原英文
}

int main() {
    // 服务器域名解析
    struct hostent *he = gethostbyname("api.seniverse.com");
    if (he == NULL) {
        perror("域名解析失败");
        return -1;
    }

    // 创建socket
    int cli_fd = socket(AF_INET, SOCK_STREAM, 0);
    if (cli_fd == -1) {
        perror("创建socket失败");
        return -1;
    }

    // 连接服务器
    struct sockaddr_in ser_inf;
    memset(&ser_inf, 0, sizeof(ser_inf));
    ser_inf.sin_family = AF_INET;
    ser_inf.sin_port = htons(80);
    ser_inf.sin_addr = *((struct in_addr *)he->h_addr_list[0]);
    
    if (connect(cli_fd, (struct sockaddr *)&ser_inf, sizeof(ser_inf)) == -1) {
        perror("连接服务器失败");
        close(cli_fd);
        return -1;
    }

    // 获取用户输入的城市
    char city[50] = "\0";
    printf("请输入要查询的城市: ");
    if (scanf("%49s", city) != 1) {
        printf("输入错误\n");
        close(cli_fd);
        return -1;
    }
     // 获取用户输入的城市
    char city[50] = "广州";
    


    // 构造HTTP请求（使用你的私钥）
    char request[1024] = "\0";
    sprintf(request,
            "GET /v3/weather/now.json?key=SN_g1fbWfLIKumOcf&location=%s&language=zh-Hans&unit=c HTTP/1.1\r\n"
            "Host: api.seniverse.com\r\n"
            "Connection: close\r\n\r\n",
            city);

    if (write(cli_fd, request, strlen(request)) == -1) {
        perror("发送请求失败");
        close(cli_fd);
        return -1;
    }

    // 读取响应头
    char stat_line[4096] = "\0";
    int pos = 0;
    ssize_t n;
    while (pos < sizeof(stat_line) - 1) {
        n = read(cli_fd, stat_line + pos, 1);
        if (n == -1) {
            perror("读取响应头失败");
            close(cli_fd);
            return -1;
        }
        if (n == 0) break;
        pos++;
        if (strstr(stat_line, "\r\n\r\n")) break;
    }

    // 检查HTTP状态码
    char *retcode = strstr(stat_line, "HTTP/1.");
    if (!retcode) {
        printf("无法解析响应状态\n");
        close(cli_fd);
        return -1;
    }
    int status = atoi(retcode + 9);
    if (status < 200 || status >= 300) {
        printf("查询失败，状态码: %d\n", status);
        close(cli_fd);
        return -1;
    }

    // 获取响应体长度
    char *obj_p = strstr(stat_line, "Content-Length: ");
    if (!obj_p) {
        close(cli_fd);
        return -1;
    }
    int content_len = atoi(obj_p + strlen("Content-Length: "));
    if (content_len <= 0) {
        close(cli_fd);
        return -1;
    }

    // 分配内存并读取响应体
    char *weather = malloc(content_len + 1);
    if (!weather) {
        perror("内存分配失败");
        close(cli_fd);
        return -1;
    }
    memset(weather, 0, content_len + 1);

    // 跳过响应头，读取响应体
    char *body_start = strstr(stat_line, "\r\n\r\n");
    int body_in_header = 0;
    if (body_start) {
        body_start += 4;
        body_in_header = pos - (body_start - stat_line);
        if (body_in_header > 0) {
            memcpy(weather, body_start, body_in_header);
            content_len -= body_in_header;
        }
    }

    int total_read = body_in_header;
    while (total_read < content_len + body_in_header) {
        n = read(cli_fd, weather + total_read, content_len - (total_read - body_in_header));
        if (n == -1) {
            perror("读取响应体失败");
            free(weather);
            close(cli_fd);
            return -1;
        }
        if (n == 0) break;
        total_read += n;
    }

    // 解析JSON并提取关键信息
    cJSON *json_data = cJSON_Parse(weather);
    if (json_data) {
        // 注意API返回的键名是首字母大写（Results/Location/Now）
        cJSON *results = cJSON_GetObjectItem(json_data, "Results");
        if (results && cJSON_IsArray(results) && cJSON_GetArraySize(results) > 0) {
            cJSON *first_result = cJSON_GetArrayItem(results, 0);
            cJSON *location = cJSON_GetObjectItem(first_result, "Location");
            cJSON *now = cJSON_GetObjectItem(first_result, "Now");
            
            if (location && now) {
                cJSON *name = cJSON_GetObjectItem(location, "Name");
                cJSON *text = cJSON_GetObjectItem(now, "Text");
                cJSON *temperature = cJSON_GetObjectItem(now, "Temperature");
                
                if (name && text && temperature) {
                    // 仅输出需要的中文信息
                    printf("城市: %s, 天气: %s, 温度: %s°C\n",
                           name->valuestring,
                           weather_en_to_cn(text->valuestring),
                           temperature->valuestring);
                }
            }
        }
        cJSON_Delete(json_data);
    }

    // 清理资源
    free(weather);
    close(cli_fd);
    return 0;
}
    */